package pei.lei.lostandfound.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;
import pei.lei.lostandfound.base.utils.ResultUtil;
import pei.lei.lostandfound.base.dto.ResultDTO;
import pei.lei.lostandfound.base.page.PageList;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import pei.lei.lostandfound.service.UserInfoService;
import pei.lei.lostandfound.entity.UserInfo;

import java.util.List;


/**
 *
 * @author Zhang
 * @since 2021-01-18
 */
@RestController
@RequestMapping("/userInfo")
@CrossOrigin
@Slf4j
public class UserInfoController {

        @Autowired
        private UserInfoService userInfoService;


        @GetMapping("{id}")
        @ApiOperation("按用户id数据")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "path", name = "id", dataType = "string", required = true, value = "实体类id"),

        })
        public ResultDTO selectById(@PathVariable("id") String id)throws Exception{
            UserInfo oldUserInfo = null;
            try {
                oldUserInfo = userInfoService.get(id);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(oldUserInfo);
        }



        @PostMapping("/insertUserInfo")
        @ApiOperation(value = "插入一条数据")
        //@ApiImplicitParams()
        public ResultDTO insertOne( @RequestBody UserInfo userInfo)throws Exception{
            try {
                userInfoService.save(userInfo);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(userInfo);
        }

        @PostMapping("/updateById")
        @ApiOperation(value = "按id修改数据,除了用户Id，改什么传什么参数")
        @ApiImplicitParams({
                @ApiImplicitParam( name = "userId", dataType = "string"/*, required = true*/, value = "用户账号"),
                @ApiImplicitParam( name = "userName", dataType = "string", value = "用户名"),
                @ApiImplicitParam( name = "callphone", dataType = "string",value = "电话号码"),
                @ApiImplicitParam( name = "gender",dataType = "Integer",value = "性别，0是女，1是男")
        })
        public ResultDTO updateById(@RequestBody  UserInfo userInfo)throws Exception{
            try {
                userInfoService.update(userInfo);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(userInfoService.get(userInfo.getUserId()));

        }

        @GetMapping("/deleteById/{id}")
        @ApiOperation("按id删除数据")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "path", name = "id", dataType = "string", required = true, value = "实体类id"),

        })
        public ResultDTO deleteById(@PathVariable("id") String id)throws Exception{
            UserInfo oldUserInfo = null;
            try {
                oldUserInfo = userInfoService.get(id);
                userInfoService.deleteById(id);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(oldUserInfo);
        }

        @PostMapping("/getUserInfoPage")
        @ApiOperation("按条件查询，分页")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "query", name = "pageNum", dataType = "Integer", defaultValue = "1", required = false, value = "页码"),
                @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "Integer", defaultValue = "10", required = false, value = "每页容量")
        })
        public ResultDTO selectEntityPage(@RequestBody UserInfo userInfo,
                                          @RequestParam(value = "pageNum", defaultValue = "1", required=false ) int pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "10", required=false )int pageSize) throws Exception{
            PageList<UserInfo> pageData = null;
            try {
                pageData = userInfoService.findPageData(userInfo, pageNum, pageSize);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(pageData);

        }

        @PostMapping("/getUserInfo")
        @ApiOperation("按条件查询，不分页")
        //@ApiImplicitParams()
        public ResultDTO selectEntityPage( @RequestBody UserInfo userInfo)throws Exception{
            List<UserInfo> userInfoList = null;
            try {
                userInfoList = userInfoService.findByParams(userInfo);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(userInfoList);
        }


}
